import { Certificate, CertificateCreate, CertificateUpdate } from "../types/Certificate";

import axiosInstance from "./axios";

const API_URL = "/certificates";

const CertificateAPI = {
    getAll: async (): Promise<{ data: Certificate[] }> => {
        const res = await axiosInstance.get(API_URL);
        return res.data;
    },

    create: async (cert: CertificateCreate): Promise<{ data: Certificate }> => {
        const res = await axiosInstance.post(API_URL, cert);
        return res.data;
    },

    update: async (id: number, cert: CertificateUpdate): Promise<{ data: Certificate }> => {
        const res = await axiosInstance.put(`${API_URL}/${id}`, cert);
        return res.data;
    },

    delete: async (id: number): Promise<{ data: any }> => {
        const res = await axiosInstance.delete(`${API_URL}/${id}`);
        return res.data;
    },

    // 如果后端有部署/更新证书的接口，可以扩展
    deploy: async (id: number): Promise<{ data: { message: string } }> => {
        const res = await axiosInstance.post(`${API_URL}/${id}/deploy`);
        return res.data;
    },

    apply(data: { domain: string; host: string }) {
        return axiosInstance.post(`${API_URL}/apply`, data);
    }

};

export default CertificateAPI;
